<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="90px"
    >
      <el-form-item label="用户类型" prop="userType">
        <el-select
          v-model="queryParams.userType"
          @change="handleUserTypeChange"
          placeholder="请选择用户类型"
          class="!w-240px"
        >
          <el-option
            v-for="dict in common_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <!--        <el-radio-group-->
        <!--          v-model="queryParams.userType"-->
        <!--          @change="handleUserTypeChange"-->
        <!--          class="!w-240px"-->
        <!--        >-->
        <!--          <el-radio v-for="dict in common_user_type" :key="dict.value" :value="dict.value">-->
        <!--            {{ dict.label }}-->
        <!--          </el-radio>-->
        <!--        </el-radio-group>-->
      </el-form-item>
      <el-form-item label="登录类型" prop="loginType">
        <el-select
          v-model="queryParams.loginType"
          placeholder="请选择登录类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in system_user_login_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="用户账号" prop="loginName">
        <el-input
          v-model="queryParams.loginName"
          placeholder="请输入用户账号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否成功" prop="success">
        <el-select
          v-model="queryParams.success"
          placeholder="请选择是否成功"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in common_bool_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态码" prop="statusCode">
        <el-input
          v-model="queryParams.statusCode"
          placeholder="请输入状态码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="登录ip" prop="ip">
        <el-input
          v-model="queryParams.ip"
          placeholder="请输入登录ip"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="登录时间" prop="loginTime">
        <el-date-picker
          v-model="queryParams.loginTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table v-loading="listLoading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="日志编号" prop="id" width="120" />
      <el-table-column label="用户类型" prop="userType" width="90">
        <template #default="scope">
          <el-tooltip v-if="scope.row.userId" :content="scope.row.userId">
            <dict-tag :options="common_user_type" :value="scope.row.userType" />
          </el-tooltip>
          <dict-tag v-else :options="common_user_type" :value="scope.row.userType" />
        </template>
      </el-table-column>
      <el-table-column label="登录类型" prop="loginType" width="130" :show-overflow-tooltip="false">
        <template #default="scope">
          <dict-tag :options="system_user_login_type" :value="scope.row.loginType" />
        </template>
      </el-table-column>
      <el-table-column label="用户账号" prop="loginName" />
      <el-table-column label="登录ip" prop="ip" width="140" />
      <el-table-column label="登录地点" prop="location" />
      <!--      <el-table-column label="浏览器UA" prop="userAgent" />-->
      <el-table-column label="浏览器类型" prop="browser" />
      <el-table-column label="操作系统" prop="os" />
      <!--      <el-table-column label="扩展信息" prop="extension" />-->
      <!--      <el-table-column label="链路追踪编号" prop="traceId" />-->
      <el-table-column label="是否成功" prop="success">
        <template #default="scope">
          <el-tag v-if="scope.row.success == BoolFlagEnum.Yes" type="success" disable-transitions>
            成功
          </el-tag>
          <el-tag v-else type="danger" disable-transitions>{{ scope.row.statusCode }}</el-tag>
        </template>
      </el-table-column>
      <!--      <el-table-column label="状态码" prop="statusCode" />-->
      <el-table-column label="提示信息" prop="msg" />
      <el-table-column label="登录耗时" prop="timeCost" width="90px" />
      <el-table-column label="登录时间" prop="loginTime" width="180px" />
      <!--      <el-table-column label="创建时间" prop="createTime" width="180px" />-->
      <el-table-column label="操作" width="80px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openDetail(scope.row)"
            v-hasPermi="['system:login-log:query']"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：详情 -->
  <LoginLogDetail ref="detailRef" />
</template>

<script setup lang="ts">
import { LoginLogApi, LoginLogVO } from '@/api/system/login_log'
import LoginLogDetail from './LoginLogDetail.vue'
import { BoolFlagEnum, UserTypeEnum } from '@/utils/constants'

/** 用户登录日志 列表 */
defineOptions({ name: 'SystemLoginLog' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { common_user_type, common_bool_flag } = useDict(
  DICT_TYPE.common_user_type,
  DICT_TYPE.common_bool_flag
)
const { system_user_login_type } = useIntDict(DICT_TYPE.system_user_login_type)

const listLoading = ref(true) // 列表的加载中状态
const list = ref<LoginLogVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  loginType: undefined,
  userId: undefined,
  userType: UserTypeEnum.Staff,
  loginName: undefined,
  success: undefined,
  statusCode: undefined,
  ip: undefined,
  loginTime: [],
  pageNo: 1,
  pageSize: 20
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  listLoading.value = true
  try {
    const data = await LoginLogApi.getLoginLogPage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  // 用户类型条件不重置
  const userType = queryParams.userType
  queryFormRef.value.resetFields()
  queryParams.userType = userType
  handleQuery()
}

const handleUserTypeChange = () => {
  handleQuery()
}

/** 详情 */
const detailRef = ref<InstanceType<typeof LoginLogDetail>>()
const openDetail = (data: LoginLogVO) => {
  detailRef.value?.open(data)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss"></style>
